package com.renwei.yunlong.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.InitiatedWorkActivity;
import com.renwei.yunlong.activity.MeUpcomingActivity;
import com.renwei.yunlong.adapter.WorkAdapter;
import com.renwei.yunlong.chartfragment.BarChartFragment;
import com.renwei.yunlong.chartfragment.HBarChartFragment;
import com.renwei.yunlong.chartfragment.PieChartFragment;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.SetIndicatorUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 工单--Fragemnt
 *
 * Created by Administrator on 2017/8/24.
 */

public class WorkOrderFragment extends Fragment implements View.OnClickListener{

    private List<Fragment> list;
    private String[] titles = {"解决率", "SLA", "满意度", "投诉率"};

    private WorkOrderAdapter adapter;
    private PopupWindow popupWindow;

    private TextView tvTitle;
    private ImageView ivTir;
    private ImageView ivEdit;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private ImageView ivUpcoming;//待办
    private ImageView ivHandled;//经办
    private RelativeLayout rlTitle;

    private Intent intent;
    private ACache mCache;

    boolean isClick= false;//标记
    View viewFragment;

    public WorkAdapter workAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewFragment = inflater.inflate(R.layout.fragment_work_order, container, false);

        StatusBarUtil.setColor(getActivity(), CommonUtils.getColor(getActivity(), R.color.title_color), 0);
        mCache = ACache.get(getActivity());
        //屏幕自适配
        AutoUtils.setSize(getActivity(), false, 720, 1280);
        AutoUtils.auto(viewFragment);
        initView(viewFragment);
        return viewFragment;
    }


    private void initView(View view){

        tvTitle = (TextView) view.findViewById(R.id.tv_title);
        ivTir = (ImageView) view.findViewById(R.id.iv_tir);
        ivEdit = (ImageView) view.findViewById(R.id.iv_edit);
        mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        mViewPager = (ViewPager) view.findViewById(R.id.v_pager);
        ivUpcoming = (ImageView) view.findViewById(R.id.iv_upcoming);
        ivHandled = (ImageView) view.findViewById(R.id.iv_handled);
        rlTitle = (RelativeLayout) view.findViewById(R.id.rl_title);

        initData();

    }



    public void initData() {
        tvTitle.setOnClickListener(this);
        ivEdit.setOnClickListener(this);

        ivUpcoming.setOnClickListener(this);
        ivHandled.setOnClickListener(this);

        //页面，数据源
        list = new ArrayList<>();
        list.add(new PieChartFragment());
        list.add(new BarChartFragment());
        list.add(new HBarChartFragment());
        list.add(new PieChartFragment());

        //ViewPager的适配器
        adapter = new WorkOrderAdapter(getFragmentManager());
        mViewPager.setAdapter(adapter);
        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                SetIndicatorUtils.setIndicator(mTabLayout, 20, 20);
            }
        });
        //绑定
        mTabLayout.setupWithViewPager(mViewPager);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_title:
                if (isClick == false){

                    ivTir.setImageResource(R.mipmap.up);
                    isClick = true;
                    showPopuWindow(viewFragment);

                }else if (isClick == true){
                    ivTir.setImageResource(R.mipmap.tri);
                    isClick = false;
                    popupWindow.dismiss();
                }

                break;
            case R.id.iv_edit:
                intent = new Intent(getActivity(), InitiatedWorkActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_upcoming://待办
                intent = new Intent(getActivity(), MeUpcomingActivity.class);
                intent.putExtra("upcoming","upcoming");
                startActivity(intent);

                break;
            case R.id.iv_handled://经办
                intent = new Intent(getActivity(), MeUpcomingActivity.class);
                intent.putExtra("handled","handled");
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    //popupWindow弹框
    private void showPopuWindow(View view) {
        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.popu_work_order_layout, null);
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        workAdapter = new WorkAdapter(getActivity());
        workAdapter.setOnItemClickListener(new WorkAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String title) {
                tvTitle.setText(title);
                workAdapter.setSelection(position);
                workAdapter.notifyDataSetChanged();
                popupWindow.dismiss();
            }
        });
        recyclerView.setAdapter(workAdapter);

//        backgroundAlpha(getActivity(), 0.5f);//0.0-1.0
//        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                // TODO Auto-generated method stub
//                backgroundAlpha(getActivity(), 1f);
//            }
//        });

        ColorDrawable dw = new ColorDrawable(0xb0808080);
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.showAsDropDown(rlTitle, 0, 0);
        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha) {

        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);

    }

    class WorkOrderAdapter extends FragmentStatePagerAdapter {

        public WorkOrderAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        //重写这个方法，将设置每个Tab的标题
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
